﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Interactivity;
using nRoute.Components;
#if (WINRT)
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using nRoute.Behaviors.Interactivity;
#else
using System.Windows.Data;
#endif

namespace nRoute.Behaviors
{
    public class BridgeValueConverterBehavior
#if (WINRT)
        : Behavior<FrameworkElement>
#else
        : Behavior<DependencyObject>
#endif
    {
        public static readonly DependencyProperty ValueConverterRelayProperty =
            DependencyProperty.Register("ValueConverterRelay", typeof(ValueConverterRelay), typeof(BridgeValueConverterBehavior),
                new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE, OnValueConverterRelayChanged));

        public static readonly DependencyProperty ConverterSourceProperty =
            DependencyProperty.Register("ConverterSource", typeof(IValueConverter), typeof(BridgeValueConverterBehavior),
                new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE, OnConverterSourceChanged));

#region Properties

        [Category("Common Properties")]
        public ValueConverterRelay ValueConverterRelay 
        {
            get { return (ValueConverterRelay)GetValue(ValueConverterRelayProperty); }
            set { SetValue(ValueConverterRelayProperty, value); }
        }

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public IValueConverter ConverterSource
        {
            get {return (IValueConverter)GetValue(ConverterSourceProperty); }
            set { SetValue(ConverterSourceProperty, value);}
        }

#if (WINRT)

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding ValueConverterRelayBinding
        {
            get { return GetBinding(ValueConverterRelayProperty); }
            set { SetBinding<ValueConverterRelay>(ValueConverterRelayProperty, value); }
        }

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding ConverterSourceBinding
        {
            get { return GetBinding(ConverterSourceProperty); }
            set { SetBinding<IValueConverter>(ConverterSourceProperty, value); }
        }

#endif

#endregion

#region Handlers

        private static void OnConverterSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((BridgeValueConverterBehavior)d).UpdateRelay();
        }

        private static void OnValueConverterRelayChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((BridgeValueConverterBehavior)d).UpdateRelay();
        }

#endregion

#region Overrides

        protected override void OnAttached()
        {
            base.OnAttached();
            UpdateRelay();
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
        }

#endregion

#region Helpers
        
        private void UpdateRelay()
        {
            if (ValueConverterRelay != null && this.ConverterSource != null)
            {
                ValueConverterRelay.Converter = ConverterSource;
            }
        }

#endregion

    }
}
